package to4.client;

import java.util.ArrayList;

import com.google.gwt.maps.client.MapWidget;
import com.google.gwt.maps.client.control.LargeMapControl;
import com.google.gwt.maps.client.geocode.DirectionQueryOptions;
import com.google.gwt.maps.client.geocode.DirectionResults;
import com.google.gwt.maps.client.geocode.Directions;
import com.google.gwt.maps.client.geocode.DirectionsCallback;
import com.google.gwt.maps.client.geocode.DirectionsPanel;
import com.google.gwt.maps.client.geocode.Waypoint;
import com.google.gwt.maps.client.geom.LatLng;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class Routeplanner {
	private MetHome MH;
	private MapWidget map;
	private VerticalPanel vp;
	DirectionQueryOptions opts;
	DirectionsPanel directionsPanel;
	public Routeplanner(MetHome thema1){
		this.MH = thema1;
	}
	public Panel createPage(String email){
		vp = new VerticalPanel();
		final FlowPanel eindPanel = new FlowPanel();
		directionsPanel = new DirectionsPanel();
		Label routeplannerTitel = new Label("De route naar Alpe d'Huez");
		routeplannerTitel.addStyleName("routeplannerTitel");
		eindPanel.add(routeplannerTitel);
		AsyncCallback<ArrayList<String>> plaatsen = new AsyncCallback<ArrayList<String>>() {
			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
			}
			@Override
			public void onSuccess(ArrayList<String> result) {
				System.out.println("Routeplanner");
				for (String s:result){
					System.out.println(s);
				}
				if (result.size()==1){
					Waypoint waypoints2[] = {
							new Waypoint(result.get(0)+", Nederland"),
							new Waypoint("Alpe d'Huez, Huez, Frankrijk")
					};

					System.out.println(waypoints2.length);
					LatLng somewhere = LatLng.newInstance(52.103355, 5.106566);

					map = new MapWidget(somewhere, 10);
					map.setSize("640px", "480px");
					map.addControl(new LargeMapControl());

					opts = new DirectionQueryOptions(map, directionsPanel);

					Directions.loadFromWaypoints(waypoints2, opts, new DirectionsCallback() {
						public void onFailure(int statusCode) {
							Window.alert("Verkeerd adres, foutmelding: " + statusCode);
						}
						public void onSuccess(DirectionResults result) {
						}
					});


					waypoints2 = null;
				};
				if (result.size()==2){
					Waypoint waypoints2[] = {
							new Waypoint(result.get(0)+", Nederland"),
							new Waypoint(result.get(1)+", Nederland"),
							new Waypoint("Alpe d'Huez, Huez, Frankrijk")
					};
					LatLng somewhere = LatLng.newInstance(52.103355, 5.106566);

					map = new MapWidget(somewhere, 10);
					map.setSize("640px", "480px");
					map.addControl(new LargeMapControl());
					directionsPanel.setSize("90%", "100%");
					opts = new DirectionQueryOptions(map, directionsPanel);

					Directions.loadFromWaypoints(waypoints2, opts, new DirectionsCallback() {
						public void onFailure(int statusCode) {
							Window.alert("Verkeerd adres, foutmelding: " + statusCode);
						}
						public void onSuccess(DirectionResults result) {
						}
					});

					waypoints2 = null;
				};
				if (result.size()==3){
					Waypoint waypoints2[] = {
							new Waypoint(result.get(0)+", Nederland"),
							new Waypoint(result.get(1)+", Nederland"),
							new Waypoint(result.get(2)+", Nederland"),
							new Waypoint("Alpe d'Huez, Huez, Frankrijk")
					};
					LatLng somewhere = LatLng.newInstance(52.103355, 5.106566);

					map = new MapWidget(somewhere, 10);
					map.setSize("640px", "480px");
					map.addControl(new LargeMapControl());
					directionsPanel.setSize("90%", "100%");
					opts = new DirectionQueryOptions(map, directionsPanel);
					Directions.loadFromWaypoints(waypoints2, opts, new DirectionsCallback() {
						public void onFailure(int statusCode) {
							Window.alert("Verkeerd adres, foutmelding: " + statusCode);
						}
						public void onSuccess(DirectionResults result) {
						}
					});

					waypoints2 = null;
				};
				if (result.size()==4){
					Waypoint waypoints2[] = {
							new Waypoint(result.get(0)+", Nederland"),
							new Waypoint(result.get(1)+", Nederland"),
							new Waypoint(result.get(2)+", Nederland"),
							new Waypoint(result.get(3)+", Nederland"),
							new Waypoint("Alpe d'Huez, Huez, Frankrijk")
					};
					LatLng somewhere = LatLng.newInstance(52.103355, 5.106566);

					map = new MapWidget(somewhere, 10);
					map.setSize("640px", "480px");
					map.addControl(new LargeMapControl());
					directionsPanel.setSize("90%", "100%");
					opts = new DirectionQueryOptions(map, directionsPanel);
					Directions.loadFromWaypoints(waypoints2, opts, new DirectionsCallback() {
						public void onFailure(int statusCode) {
							Window.alert("Verkeerd adres, foutmelding: " + statusCode);
						}
						public void onSuccess(DirectionResults result) {
						}
					});

					waypoints2 = null;
				};
				if (result.size()==5){
					Waypoint waypoints2[] = {
							new Waypoint(result.get(0)+", Nederland"),
							new Waypoint(result.get(1)+", Nederland"),
							new Waypoint(result.get(2)+", Nederland"),
							new Waypoint(result.get(3)+", Nederland"),
							new Waypoint(result.get(4)+", Nederland"),
							new Waypoint("Alpe d'Huez, Huez, Frankrijk")
					};
					LatLng somewhere = LatLng.newInstance(52.103355, 5.106566);

					map = new MapWidget(somewhere, 10);
					map.setSize("640px", "480px");
					map.addControl(new LargeMapControl());
					directionsPanel.setSize("90%", "100%");
					opts = new DirectionQueryOptions(map, directionsPanel);
					Directions.loadFromWaypoints(waypoints2, opts, new DirectionsCallback() {
						public void onFailure(int statusCode) {
							Window.alert("Verkeerd adres, foutmelding: " + statusCode);
						}
						public void onSuccess(DirectionResults result) {
						}
					});

					waypoints2 = null;
				};
				if (result.size()==6){
					Waypoint waypoints2[] = {
							new Waypoint(result.get(0)+", Nederland"),
							new Waypoint(result.get(1)+", Nederland"),
							new Waypoint(result.get(2)+", Nederland"),
							new Waypoint(result.get(3)+", Nederland"),
							new Waypoint(result.get(4)+", Nederland"),
							new Waypoint(result.get(5)+", Nederland"),
							new Waypoint("Alpe d'Huez, Huez, Frankrijk")
					};
					LatLng somewhere = LatLng.newInstance(52.103355, 5.106566);

					map = new MapWidget(somewhere, 10);
					map.setSize("640px", "480px");
					map.addControl(new LargeMapControl());
					directionsPanel.setSize("90%", "100%");
					opts = new DirectionQueryOptions(map, directionsPanel);
					Directions.loadFromWaypoints(waypoints2, opts, new DirectionsCallback() {
						public void onFailure(int statusCode) {
							Window.alert("Verkeerd adres, foutmelding: " + statusCode);
						}
						public void onSuccess(DirectionResults result) {
						}
					});

					waypoints2 = null;
				};
				if (result.size()==7){
					Waypoint waypoints2[] = {
							new Waypoint(result.get(0)+", Nederland"),
							new Waypoint(result.get(1)+", Nederland"),
							new Waypoint(result.get(2)+", Nederland"),
							new Waypoint(result.get(3)+", Nederland"),
							new Waypoint(result.get(4)+", Nederland"),
							new Waypoint(result.get(5)+", Nederland"),
							new Waypoint(result.get(6)+", Nederland"),
							new Waypoint("Alpe d'Huez, Huez, Frankrijk")
					};
					LatLng somewhere = LatLng.newInstance(52.103355, 5.106566);

					map = new MapWidget(somewhere, 10);
					map.setSize("640px", "480px");
					map.addControl(new LargeMapControl());
					directionsPanel.setSize("90%", "100%");
					opts = new DirectionQueryOptions(map, directionsPanel);
					Directions.loadFromWaypoints(waypoints2, opts, new DirectionsCallback() {
						public void onFailure(int statusCode) {
							Window.alert("Verkeerd adres, foutmelding: " + statusCode);
						}
						public void onSuccess(DirectionResults result) {
						}
					});

					waypoints2 = null;
				};
				if (result.size()==8){
					Waypoint waypoints2[] = {
							new Waypoint(result.get(0)+", Nederland"),
							new Waypoint(result.get(1)+", Nederland"),
							new Waypoint(result.get(2)+", Nederland"),
							new Waypoint(result.get(3)+", Nederland"),
							new Waypoint(result.get(4)+", Nederland"),
							new Waypoint(result.get(5)+", Nederland"),
							new Waypoint(result.get(6)+", Nederland"),
							new Waypoint(result.get(7)+", Nederland"),
							new Waypoint("Alpe d'Huez, Huez, Frankrijk")
					};
					LatLng somewhere = LatLng.newInstance(52.103355, 5.106566);

					map = new MapWidget(somewhere, 10);
					map.setSize("640px", "480px");
					map.addControl(new LargeMapControl());
					directionsPanel.setSize("90%", "100%");
					opts = new DirectionQueryOptions(map, directionsPanel);
					Directions.loadFromWaypoints(waypoints2, opts, new DirectionsCallback() {
						public void onFailure(int statusCode) {
							Window.alert("Verkeerd adres, foutmelding: " + statusCode);
						}
						public void onSuccess(DirectionResults result) {
						}
					});

					waypoints2 = null;
				};
				if (result.size()==9){
					Waypoint waypoints2[] = {
							new Waypoint(result.get(0)+", Nederland"),
							new Waypoint(result.get(1)+", Nederland"),
							new Waypoint(result.get(2)+", Nederland"),
							new Waypoint(result.get(3)+", Nederland"),
							new Waypoint(result.get(4)+", Nederland"),
							new Waypoint(result.get(5)+", Nederland"),
							new Waypoint(result.get(6)+", Nederland"),
							new Waypoint(result.get(7)+", Nederland"),
							new Waypoint(result.get(8)+", Nederland"),
							new Waypoint("Alpe d'Huez, Huez, Frankrijk")
					};
					LatLng somewhere = LatLng.newInstance(52.103355, 5.106566);
					map = new MapWidget(somewhere, 10);
					map.setSize("640px", "480px");
					map.addControl(new LargeMapControl());
					directionsPanel.setSize("90%", "100%");
					opts = new DirectionQueryOptions(map, directionsPanel);
					opts.setLocale("nl_NL");
					Directions.loadFromWaypoints(waypoints2, opts, new DirectionsCallback() {
						public void onFailure(int statusCode) {
							Window.alert("Verkeerd adres, foutmelding: " + statusCode);
						}
						public void onSuccess(DirectionResults result) {

						}

					});
					waypoints2 = null;
				};
				vp.add(map);
				vp.add(directionsPanel);
				vp.addStyleName("routeplannerlayoutright");

				eindPanel.add(vp);
			};
		};
		MH.thema.myService.getPlaats(email, plaatsen);
		return eindPanel;
	}
}

